package QuickSort;

public class Text {
    //快速排序
    public static void main(String[] args) {
        int[] arr = {3,56, 7, 2, 34, 5, 6, 87, 8,};

        quickSort(arr,0,arr.length-1);

        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }

    private static void quickSort(int[] arr, int i, int j) {

        int start = i;
        int end = j;

        if(start > end){
            return;
        }


        //基准数
        int baseNumber = arr[i];

        //利用循环找到要交换的数字
        while(start != end){
            //利用end，从后往前找 比基准数小的数字
            while (true){
                if(start >= end || arr[end] < baseNumber)
                break;
                end--;
            }


            //利用start，从前往后找 比基准数大的数字
            while (true){
                if(start >= end || arr[start] > baseNumber)
                break;
                start++;
            }
            int temp = arr[start];
            arr[start] = arr[end];
            arr[end] = temp;
        }

        int temp = arr[i];
        arr[i] = arr[start];
        arr[start] = temp;

        //myquickSort(arr,0,arr.length);

        quickSort(arr,i,start-1);

        quickSort(arr,start+1,j);
    }
}
